package com.richy.toweather.adapter;

import android.content.Context;
import android.graphics.PorterDuff;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.richy.toweather.R;
import com.richy.toweather.entity.WeatherEntity;

import java.util.List;

/**
 * @FileName WeatherRecyclerAdapter
 * @Description WeatherRecyclerAdapter
 * @Author x925914554@gmail.com
 * @Company
 * @CreateDate 18-11-13
 * @LastModifyDate 18-11-13
 * @Since
 * @Version
 */
public class WeatherRecyclerAdapter extends RecyclerView.Adapter<WeatherRecyclerAdapter.ItemWeatherViewHolder> {
    public static final Integer[] COLOR_DATAS = new Integer[]{
            R.color.color_1,
            R.color.color_2,
            R.color.color_3,
            R.color.color_4,
            R.color.color_5,
            R.color.color_6,
            R.color.color_7,
            R.color.color_8,
            R.color.color_9,
            R.color.color_10,
            R.color.color_11,
            R.color.color_12,
            R.color.color_13,
            R.color.color_14,
            R.color.color_15,
            R.color.color_16,
            R.color.color_17,
            R.color.color_18,
            R.color.color_19,
            R.color.color_20,
            R.color.color_21,
            R.color.color_22,
            R.color.color_23,
            R.color.color_24,
            R.color.color_25,
            R.color.color_26
    };

    private Context mContext;

    private List<WeatherEntity> mWeatherEntityList;

    public WeatherRecyclerAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public ItemWeatherViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        final View inflate = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_city_weather, viewGroup, false);
        return new ItemWeatherViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final ItemWeatherViewHolder viewHolder, int position) {
        viewHolder.mLayout.getBackground().setColorFilter(ContextCompat.getColor(mContext, COLOR_DATAS[position % 26]), PorterDuff.Mode.SRC_IN);
        WeatherEntity data = mWeatherEntityList.get(position);
        WeatherEntity.HeWeather6Bean.NowBean now = data.getHeWeather6().get(0).getNow();
        viewHolder.mLocation.setText(data.getHeWeather6().get(0).getBasic().getLocation());
        viewHolder.mTmp.setText(now.getTmp());
        viewHolder.mCondTxt.setText(now.getCond_txt());
        viewHolder.mLoc.setText(data.getHeWeather6().get(0).getUpdate().getLoc());

        viewHolder.mFl.setText(now.getFl() + " ℃");
        viewHolder.mWindDir.setText(now.getWind_dir());
        viewHolder.mWindSc.setText(now.getWind_sc() + " 级");
        viewHolder.mWindSpd.setText(now.getWind_spd() + " km/h");
        viewHolder.mHum.setText(now.getHum() + " %");
        viewHolder.mPcpn.setText(now.getPcpn() + " mm");
        viewHolder.mPres.setText(now.getPres() + " Pa");
        viewHolder.mVis.setText(now.getVis() + " km");
        viewHolder.mCloud.setText(now.getCloud() + " %");

        viewHolder.mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.mContainerContent.getVisibility() == View.VISIBLE) {
                    viewHolder.mContainerContent.setVisibility(View.GONE);
                } else {
                    viewHolder.mContainerContent.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        if (mWeatherEntityList != null) {
            return mWeatherEntityList.size();
        }
        return 0;
    }

    public void setWeatherEntityList(List<WeatherEntity> weatherEntityList) {
        mWeatherEntityList = weatherEntityList;
        notifyDataSetChanged();
    }

    public static class ItemWeatherViewHolder extends RecyclerView.ViewHolder {
        View mLayout;
        View mContainerContent;

        TextView mLocation;
        TextView mTmp;
        TextView mCondTxt;
        TextView mLoc;

        TextView mFl;
        TextView mWindDir;
        TextView mWindSc;
        TextView mWindSpd;
        TextView mHum;
        TextView mPcpn;
        TextView mPres;
        TextView mVis;
        TextView mCloud;

        public ItemWeatherViewHolder(@NonNull View view) {
            super(view);
            mLayout = view.findViewById(R.id.fl_main_card);
            mContainerContent = view.findViewById(R.id.ll_content);

            mLocation = view.findViewById(R.id.tv_location);
            mTmp = view.findViewById(R.id.tv_tmp);
            mCondTxt = view.findViewById(R.id.tv_weather);
            mLoc = view.findViewById(R.id.tv_time);

            mFl = view.findViewById(R.id.tv_fl);
            mWindDir = view.findViewById(R.id.tv_wind_dir);
            mWindSc = view.findViewById(R.id.tv_wind_sc);
            mWindSpd = view.findViewById(R.id.tv_wind_spd);
            mHum = view.findViewById(R.id.tv_hum);
            mPcpn = view.findViewById(R.id.tv_pcpn);
            mPres = view.findViewById(R.id.tv_pres);
            mVis = view.findViewById(R.id.tv_vis);
            mCloud = view.findViewById(R.id.tv_cloud);
        }
    }
}
